
<template>
	<div class="index">

		<div class="center">
			<left></left>
			<div class="right">
          <top></top>
          <!-- <div class="hei10"></div>
          <div class="where" style="background: #fff; padding: 10px 30px; font-size: 14px; color: #595959;">当前位置：分销后台><router-link to="/orderImport">导入订单</router-link></div>
          --><div class="hei10"></div>
        <div class="o_ul">
					<router-link class="o_li act" to="/orderImport">批量导入</router-link>
					
					<router-link class="o_li" to="/orderImport/order_hand">手动创建</router-link>
					
				</div>
        <div style="background: rgb(218, 231, 245); color: rgb(100, 112, 118); padding: 15px 20px; font-size: 14px; margin-bottom: 20px;">订单导入后如果订单来源和外部订单号都真实存在，则会默认同步到电商平台发货，如果导入订单不想同步发货请修改订单来源或外部单号</div>
        <div class="con">
          <div class="kflex" style="margin:0 20px; padding: 20px 0; border-bottom: 1px #ddd dashed;align-items: center;">
            <el-upload class="upload-demo" action="/api/store/store-order/import" :http-request="ordergo"
              :before-upload="shangchuanexcel">
              <el-button type="danger" round style="width: 138px; text-align: center;">导入订单</el-button>
            </el-upload>
            <div class="develop-tcldown">
              <a class="unload" @click="uploadTemplete">下载模板</a>
            </div>

            <el-upload class="upload-demo" action="/api/system/upload_pic_zip" :http-request="picgo" style="margin-left: 50px;"
              :before-upload="shangchuanzip">
              <el-button  type="danger" round style="width: 138px; text-align: center;">上传照片</el-button>
            </el-upload>
            <div class="develop-tcldown">
              <a class="unload" @click="photoTemplete">下载模板</a>
            </div>
            <!-- <el-button  type="danger" round style="width: 178px; height: 38px; text-align: center; margin-left: 100px; margin-right:20px ;" @click="feidankai">按导入单号复制订单</el-button>
            <div class="develop-tcldown" style="color: #666 !important;">
              直接在原订单上复制一个订单，该订单所属商品和订单来源由操作人指定。
            </div> -->
            <el-upload class="upload-demo" action="#" :before-upload="feidan" >
              <el-button  type="danger" round style="width: 178px; height: 38px; text-align: center; margin-left: 50px;" >按导入单号复制订单</el-button>
            </el-upload>
            <el-popover
              style="margin-right:20px"
              placement="bottom"
              width="400"
              trigger="click">
              <div>
                <div>功能：直接在原订单上复制一个订单，该订单所属商品和订单来源由模版填写指定。</div>
                <div>注1：系统会对要复制的订单中的身份证号进行查重，如果存在身份证号重复，则只复制一单。一次导入复制操作中，一个证件号只能被复制1单</div>
                <div>注2：订单来源必须是已经在设置中定义好的来源，如果录入的订单来源不存在，会报错</div>
              </div>

              <img slot="reference"  src="../../../static/img/wen.png" alt="">
            </el-popover>

            <div class="develop-tcldown" style=" ">
              <a class="unload" @click="photoTemplete2">下载模板</a>
            </div>
            <el-button @click="tonocopy" type="primary" round plain  style="width: 178px; height: 38px; text-align: center; margin-left: 50px;" >设置不复制订单名单</el-button>
            <el-popover
              style="margin-right:20px;margin-left: 10px;"
              placement="bottom"
              width="400"
              trigger="click">
              <div>
                <div>不复制订单中的名单将不会复制，该功能按照证件号码匹配</div>
              </div>

              <img slot="reference"  src="../../../static/img/wen.png" alt="">
            </el-popover>


          </div>
          <div class="kflexc" style="margin:0 20px; padding: 20px 0; border-bottom: 1px #ddd dashed; ">
            <upload-excel-component @on-selected-file="exportData" class="btnp" round
              buttonName="身份证信息导入">
            </upload-excel-component>
            <div class="develop-tcldown" style="width: 250px; margin-left: 20px;">
              <a class="unload" @click="photoTemplete3">下载模板</a>
            </div>
          </div>
          <div class="kflexc" style="margin:0 20px; padding: 20px 0; border-bottom: 1px #ddd dashed; color:rgb(58, 58, 58);">
            <div>各平台快捷导入：</div>
            <upload-excel-component @on-selected-file="exportData" class="btnp" buttonName="导入天猫订单信息">
            </upload-excel-component>

            <upload-excel-component @on-selected-file="exportData" class="btnp" style="margin:0 30px;"
              buttonName="导入有赞订单信息">
            </upload-excel-component>

            <el-button type="danger" round @click="log = true">
              导入快手订单信息
            </el-button>
            <!-- <upload-excel-component @on-selected-file="exportData" class="btnp" buttonName="拼多多证件导入">
            </upload-excel-component> -->
            <div style="margin-left: 20px;">各平台导出表直接导入，导出有加密的请先取消加密</div>
          </div>
          <!-- <div class="develop">
            <div class="develop-top">
              <div class="develop-topcon" style="flex-wrap: wrap;">
                <div class="develop-topconleft">

                  <div class="develop-tcltow">
                    <el-upload class="upload-demo" action="/api/store/store-order/import" :http-request="ordergo"
                      :before-upload="shangchuanexcel">
                      <el-button type="primary">导入订单</el-button>
                    </el-upload>
                    <div class="develop-tcldown">
                      <a class="unload" @click="uploadTemplete">下载订单模板</a>
                    </div>
                    <el-upload class="upload-demo" action="/api/system/upload_pic_zip" :http-request="picgo"
                      :before-upload="shangchuanzip">
                      <el-button type="primary">上传照片</el-button>
                    </el-upload>
                    <div class="develop-tcldown">
                      <a class="unload" @click="photoTemplete">下载照片模板</a>
                    </div>
                    <div class="orders-alt">照片包上传后识别匹配大概需要等待30分钟</div>

                      <upload-excel-component @on-selected-file="exportData" class="btnp" style="margin-left: 30px;"
                        buttonName="身份证信息导入">
                      </upload-excel-component>
                      <div class="develop-tcldown" style="width: 250px;">
                        <a class="unload" @click="photoTemplete3">下载身份证信息模板</a>
                      </div>
                  </div>
                </div>
                <div style="margin: 0px 30px 0px 0px; color: #999; font-size: 14px; display: flex; align-items: center; padding: 0;"  class="develop-tcltow">
                  <el-button type="danger" @click="log = true">
                    快手订单导入
                  </el-button>
                  <div style="margin: 0 20px;">从快手导出后取消密码再导入。取消密码操作：文件-设置-安全性-删除密码-保存   或者直接全内容复制到另一个新建的EXCEL表中保存</div>

                  <upload-excel-component @on-selected-file="exportData" class="btnp" buttonName="导入天猫信息">
                  </upload-excel-component>
                  <upload-excel-component @on-selected-file="exportData" class="btnp" style="margin-left: 30px;"
                    buttonName="导入有赞信息">
                  </upload-excel-component>
                </div>



              </div>
              <div style="margin: 3px 20px 20px 25px; color: #999; font-size: 14px;"  class="develop-tcltow">
                <div class="develop-tcldown" >
                  <upload-excel-component @on-selected-file="exportData" class="btnp" buttonName="拼多多证件导入">
                  </upload-excel-component>
                </div>
                <div class="develop-tcldown" style=" width: 130px;">
                  <a class="unload" @click="pinduoduodown">下载拼多多模板</a>
                </div>
              </div>

            </div>

          </div> -->

          <div class="orders" v-loading="loading">
            <div style="padding: 20px 0;">
              最近3天导入记录
            </div>
            <el-table :data="lists" border style="width: 100%" align="center">
              <el-table-column prop='created_at' label="导入时间"></el-table-column>
              <el-table-column prop='total' label="导入订单数"></el-table-column>
              <el-table-column prop='success' label="成功"></el-table-column>
              <el-table-column prop='fail' label="失败"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <div class="kflexc">
                    <div style="color: #409EFF; margin: 10px; cursor: pointer;" v-if="scope.row.error_file" @click="xiazai(scope.row)">下载失败明细</div>
                  </div>
                </template>
              </el-table-column>
            </el-table>
            <div style="text-align: right;padding: 20px;">
              <el-pagination
                background
                layout="prev, pager, next"
                :page-size="20"
                @current-change="pagechange"
                :total="total">
              </el-pagination>
            </div>
            <!-- <div class="orderslist">
              <div class="ordersl orderslth">
                <div class="ordersli">导入日期</div>
                <div class="ordersli">导入订单数</div>

                <div class="ordersli">导入照片压缩包次数</div>
                <div class="ordersli">
                  <div class="ypp">已匹配照片订单数</div>
                  <div class="ypp">(包含不需要照片的订单数)</div>
                </div>
                <div class="ordersli">未匹配照片订单数</div>
                <div class="ordersli">照片人工审核失败</div>
              </div>
            </div>
            <div class="orderslist" v-for="item in list">
              <div class="ordersl">
                <div class="ordersli">{{item.c_time}}</div>
                <div class="ordersli">{{item.orders_total}}</div>

                <div class="ordersli" v-if="item.zips_total == 0">{{item.zips_total}}</div>
                <div class="ordersli cur" v-if="item.zips_total != 0" @click="gozip(item.c_time)">
                  {{item.zips_total}}
                  <span class="dpp" v-if="item.waiting_zips_total != 0">待匹配中...</span>
                </div>
                <div class="ordersli">{{item.get_photo_total}}</div>

                <div class="ordersli" v-if="item.need_photo_total == 0">{{item.need_photo_total}}</div>
                <div class="ordersli cur2" v-if="item.need_photo_total != 0" @click="gofailorder(item.c_time,150)">
                  {{item.need_photo_total}}</div>

                <div class="ordersli" v-if="item.photo_failed_total == 0">{{item.photo_failed_total}}
                </div>
                <div class="ordersli cur2" v-if="item.photo_failed_total != 0" @click="gofailorder(item.c_time,-200)">
                  {{item.photo_failed_total}}</div>
              </div>
            </div> -->
          </div>
        </div>
        <!-- 废单 -->
        <el-dialog title="通过导入复制订单" :visible.sync="feidanlog" width="560" center>
          <div class="kflexc" style="margin-bottom: 20px;">
            <div>上传表格：</div>
            <div>
              <el-upload class="upload-demo" action="#" :before-upload="feidan" >
                <el-button type="danger" round  size="small" plain>点击上传</el-button>
                <div v-if="feidanall.datas != ''">
                  已上传文件
                </div>
              </el-upload>
            </div>
            <div>
              <div class="develop-tcldown">
                <a class="unload" @click="photoTemplete2">下载模板</a>
              </div>
            </div>
          </div>
          <div class="kflexc" style="margin-bottom: 20px;">
            <div>
              <el-select v-model="feidanall.source" clearable filterable  placeholder="请选择复制后的订单所属来源">
                <el-option v-for="item in flist" :key="item.id" :label="item.source_name"  :value="item.source_name"></el-option>
              </el-select>
            </div>
            <div style="margin-left: 20px;">
              <el-button type="danger" round size="small" @click="goid('/setup/dingdanlaiyuan/dingdanlaiyuan')">管理来源</el-button>
            </div>
          </div>
          <div class="kflexc">
            <div>
              <el-select style="width: 370px;" v-model="feidanall.order_page_id" filterable  clearable placeholder="请选择复制后的订单所属商品">
              	<el-option :label="item.title" :value="item.id" v-for="(item,index) in slist" :key="item.id"></el-option>
              </el-select>
            </div>
          </div>

          <span slot="footer" class="dialog-footer">
            <el-button type="info" round @click="feidanlog = false">取 消</el-button>
            <el-button type="danger" round @click="feidango">确 定</el-button>
          </span>
        </el-dialog>

        <el-dialog title="导入复制结果" :visible.sync="feidanlog2" width="560" center>
          <div class="kflexc">
            <div class="chenggong">成功创建订单：</div><div>{{feidanjieguo.success}}条</div>
          </div>
          <div class="kflexc">
            <div class="shibai">创建订单失败：</div><div>{{feidanjieguo.error.length}}条</div><div><el-button type="text" round @click="xiazaishibai(feidanjieguo.error)">下载失败表格</el-button></div>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button type="danger" round @click="feidanlog2guanbi">确 定</el-button>
          </span>
        </el-dialog>

        <el-dialog title="上传结果" :visible.sync="centerDialogVisible" width="100" center>
          <div class="uploadhou" v-loading="uping">
            <p class="chenggong">上传成功数：{{success}}</p>
            <p class="shibai" v-if="errors != ''">失败及原因：

            </p>
            <div v-for="(val, key, index) in errors">
              <p class="shibai">{{key}}:{{val}}</p>
            </div>
          </div>
          <span slot="footer" class="dialog-footer">
            <el-button type="info" round @click="centerDialogVisible = false">取 消</el-button>
            <el-button type="danger" round @click="centerDialogVisible = false">确 定</el-button>
          </span>
        </el-dialog>
        <el-dialog title="上传结果" :visible.sync="centerDialogVisibleorder" width="100" center>
          <div class="uploadhou" v-loading="uping">
            <p class="chenggong">上传订单成功数：{{success}}</p>
            <p class="shibai" v-if="errorsorder.length!=0">上传订单失败数：{{errorsorder.length}}：
               <el-button @click="downerror" type="text">下载失败订单明细</el-button>
            </p>
          </div>
          <span slot="footer" class="dialog-footer">
            <!-- <el-button @click="centerDialogVisibleorder = false">取 消</el-button> -->
            <el-button type="danger" round @click="centerDialogVisibleorder = false">确 定</el-button>
          </span>
        </el-dialog>

        <el-dialog title="上传快手订单" center :visible.sync="log" width="600px" center>
          <div style="display: flex; align-items: center; margin-bottom: 20px;">
            <div>选择要导入的店铺：</div>
            <div>
              <el-select v-model="kuaishoudianpu" clearable placeholder="请选择店铺">
              	<el-option :label="item.shop_name" :value="item.id" v-for="(item,index) in list2" :key="item.id"></el-option>
              </el-select>
            </div>
          </div>
          <div style="display: flex; align-items: center;">
            <div>上传数据：</div>
            <div>
              <el-upload ref="upload" action="#" :before-upload="uploasd" :show-file-list="false"  style="margin-left: 30px;"
                accept=".xlsx,.xls">
                <el-button type="danger" round>
                  导入数据并上传
                </el-button>
              </el-upload>
            </div>
          </div>
        </el-dialog>
      </div>
    </div>
    <!-- <foot></foot> -->
  </div>
</template>
<script>
  import FileSaver from 'file-saver'
  import axios from 'axios'
  import top from '@/components/top.vue'
  import left from '@/components/left.vue'
  import foot from '@/components/foot.vue'
  import UploadExcelComponent from "@/components/UploadExcel/index.vue";
  import XLSX from 'xlsx';
  import {
    read,utils
  } from 'xlsx'
  export default {
    components: {
      "left": left,
      "top": top,
      "foot": foot,
      UploadExcelComponent,
    },
    name: 'orderImport',
    data() {
      return {
        msg: "hello",
        log: false,
        disid: "",
        result: "",
        uping: false,
        loading: true,

        piczip: "",
        orderexcle: "",
        list: {},
        centerDialogVisible: false,
        centerDialogVisibleorder:false,
        success: '',
        errors: [],
        errorsorder: [],
        url: "",
        dianpuid: "",
        list2:"",
        kuaishoudianpu:"",
        lists:"",
        feidanall:{
          // source:"",
          // order_page_id:"",
          datas:[]
        },
        flist:[],
        slist:[],
        feidanlog:false,
        feidanlog2:false,
        feidanjieguo:{
          success:0,
          error:[]
        },
        downerrorlist:[],
        total:0,
        searchlist:{
          page:1,
          page_size:20
        }
      }
    },
    created() {
      //地址配置,用于下载模板
      if (window.location.href.indexOf("91haoka_platform") > -1) {
        console.log("线上项目，谨慎操作。")
        this.url = window.location.origin + "/91haoka_platform/"
      } else {
        this.url = ""
      }

      this.dianpuid = JSON.parse(localStorage.getItem('SHOP')).shop_id;



    },
    mounted() {
      this.clientheight = document.documentElement.clientHeight;
      var that = this;
      this.getlist()
      this.getlist2()
      this.getlists()
      this.getflist()
    },
    methods: {
      feidanlog2guanbi:function(){
        this.feidanlog = false
        this.feidanlog2 = false
      },
      xiazai:function(item){
        var a = document.createElement('a');
        a.setAttribute('href', window.location.origin + "/" + item.error_file);
        a.click();
      },
      xiazaishibai:function(list){
        if(list != ''){
          let tableData = [["订单号", "外部订单号","复制出来的订单所属来源","复制出来的订单所属商品编码","失败原因"]];
          list.forEach ((item,index)=> {
              let rowData = []
              //导出内容的字段
              rowData = [
                  item.order_id,
                  item.source_id,
                  item.source,
                  item.goods_sku,
                  item.error_reason
              ]
              tableData.push(rowData)
          })
          console.log(XLSX)
          let ws = XLSX.utils.aoa_to_sheet(tableData)
          let wb = XLSX.utils.book_new()
          XLSX.utils.book_append_sheet(wb, ws, '失败原因') // 工作簿名称
          XLSX.writeFile(wb, '失败原因.xlsx') // 保存的文件名
        }
      },
      downerror(){
        var list = this.downerrorlist
        let tableData = [["订单号*", "订单来源", "套餐名称*","套餐编码","身份证姓名*","身份证号码*","收件人姓名*","联系电话*","收货省","收货市","收货区","收货街道","收货地址*","购买号码","购买数量(大于2自动拆单)","失败原因"]];
        list.forEach ((item,index)=> {
            let rowData = []
            //导出内容的字段
            rowData = [
                item[0],
                item[1],
                item[2],
                item[3],
                item[4],
                item[5],
                item[6],
                item[7],
                item[8],
                item[9],
                item[10],
                item[11],
                item[12],
                item[13],
                item[14],
                item[15],
            ]
            console.log(rowData)
            tableData.push(rowData)
        })
        let ws = XLSX.utils.aoa_to_sheet(tableData)
        let wb = XLSX.utils.book_new()
        XLSX.utils.book_append_sheet(wb, ws, '失败原因') // 工作簿名称
        XLSX.writeFile(wb, '失败原因.xlsx') // 保存的文件名
      },
      pagechange(val){
        console.log(val)
        this.searchlist.page = val
        this.getlists()
      },
      getlists:function(){
        // api/plan-market/order/import/log
        axios.get("/api/plan-market/order/import/log",{
          params:this.searchlist
        })
          .then(response => {
            if (response.data.msg.code == 0) {
              console.log('shuju')
              console.log(response.data.data.data)
              this.lists = response.data.data.data
              this.total = response.data.data.total
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      getlist2: function() {
        var that = this
        axios.get('/api/gth/out-store/list?source=ks')
          .then(response => {
            if (response.data.msg.code == 0) {
              that.list2 = response.data.data
            } else {
              that.$message.error(response.data.msg.info);
            }
          })
      },
      uploasd(file, fileList) {
        var that = this
        if(this.kuaishoudianpu == ''){
          this.$message.error('请先选择店铺');
          return false
        }
        let files = {
          0: file
        }
        let data = []

        let ff = this.readExcel1(files);
        ff.then((res) => {
          res.forEach(item => {
            let obj = {
              created_at: item['订单创建时间'],
              source_id: item['订单号'],
              mobile: item['收货人电话'],
              name: item['收货人姓名'],
              pay_amount: item['实付款'],
              address: item['收货地址'],
              id_card_name: item['实名姓名'],
              id_card_num: item['身份证号'],
              sku: item['SKU编码'],
              shop_order_status:item['订单状态']
            }
            if(obj.pay_amount){
              let arr = obj.pay_amount.split('¥')
              obj.pay_amount = arr[1]
            }
            data.push(obj)
          })
          this.daorulog = true
          this.daoruing = true
          axios.post("/api/plan-market/order/info/import", {
              data: data,
              source: 'ks',
              id:this.kuaishoudianpu
            })
            .then(function(response) {
              if (response.data.msg.code == 0) {
                that.kuaishoudianpu = ''

                if (response.data.data.errors == "") {
                  that.$message.success('导入成功');
                } else {
                  that.success = response.data.data.success
                  that.errors = response.data.data.errors
                  that.centerDialogVisible = true;


                }
              } else {
                that.$message.error(response.data.msg.info);
              }
            })
        })


      },

      //废单相关
      feidankai:function(){
        this.feidanall = {
          source:"",
          order_page_id:"",
          datas:[]
        }
        this.feidanlog = true
      },
      getflist: function() {
        axios.get('/api/form/channel/sources')
          .then(response => {
            if (response.data.msg.code == 0) {
              this.flist = response.data.data

            } else {
              this.$message.error(response.data.msg.info);
            }
          })
        axios.get('/api/gth/order-pages2?status=100&_page=1&_page_size=2000')
          .then(response => {
            if (response.data.msg.code == 0) {
              this.slist = response.data.data.data
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },

      feidango:function(){

        // if(this.feidanall.source == ''){
        //   this.$message.error('请选择复制后的订单所属来源');
        //   return false
        // }
        // if(this.feidanall.order_page_id == ''){
        //   this.$message.error('请选择复制后的订单所属商品');
        //   return false
        // }




        axios.post("/api/plan-market/scrap/order/import",this.feidanall)
          .then(response=> {
            if (response.data.msg.code == 0) {
              this.feidanjieguo = response.data.data

              this.feidanlog2 = true
              var errorlist = []
              for(let index in this.feidanjieguo.error){
                  console.log(this.feidanjieguo.error[index])
                  errorlist.push(this.feidanjieguo.error[index])
              }
              this.feidanjieguo.error = errorlist
              console.log(this.feidanjieguo)
            } else {
              this.$message.error(response.data.msg.info);
            }
          })
      },
      feidan(file) {
        var that = this
        let files = {
          0: file
        }

        this.feidanall.datas = []
        console.log(files)
        let ff = this.readExcel1(files);

        ff.then((res) => {
          res.forEach(item => {
            let data = []

            if(item['订单号']){
              var order_id = item['订单号']
            }else {
              var order_id =  ''
            }
            if(item['外部订单号']){
              var source_id = item['外部订单号']
            }else {
              var source_id =  ''
            }
            if(item['复制出来的订单所属来源']){
              var source = item['复制出来的订单所属来源']
            }else {
              var source =  ''
            }
            if(item['复制出来的订单所属商品编码']){
              var goods_sku = item['复制出来的订单所属商品编码']
            }else {
              var goods_sku =  ''
            }



            let obj = {
              order_id: order_id,
              source_id:source_id ,
              source: source,
              goods_sku: goods_sku,
            }

            this.feidanall.datas.push(obj)
          })
          this.feidango()
        })
        console.log('===')
        console.log(this.feidanall.datas)

        // setTimeout(()=>{

        // },1000)


      },
      readExcel1(files) {
        return new Promise((res, rej) => {
          var that = this;
          console.log(files);
          // 此处判断不严谨，思路只是根据传入的参数校验数据的完整性，可根据自己需求修改
          // 如果没有文件名
          if (files.length <= 0) {
            return;
          }

          if (!/\.(xls|xlsx)$/.test(files[0].name.toLowerCase())) {
            this.$Message.error('上传格式不正确，请上传xls或者xlsx格式');
            return;
          }

          const fileReader = new FileReader();
          fileReader.onload = (ev) => {
            try {
              const data = ev.target.result;
              const workbook = read(data, {
                type: 'binary'
              });
              // 取第一张表
              const wsname = workbook.SheetNames[0];
              // 生成json表格内容
              const ws = utils.sheet_to_json(workbook.Sheets[wsname]);
              console.log(ws);
              // 后续为自己对ws数据的处理
              res(ws)
            } catch (e) {
              return false;
              rej(e)
            }
          };
          fileReader.readAsBinaryString(files[0]);
        })

      },

      exportData: function(data) {
        var that = this;
        var list = []
        if (data.header[0] == "订单号") {
          for (var i = 0; i < data.results.length; i++) {
            var obj = {}
            if (data.results[i]['订单号'] != undefined) {
              let x = data.results[i]['订单号'].toString()
              obj['source_id'] = x.trim()
            } else {
              obj['source_id'] = ''
            }
            if (data.results[i]['收货人手机号/提货人手机号'] != undefined) {
              let x = data.results[i]['收货人手机号/提货人手机号'].toString()
              obj['mobile'] = x.trim()
            } else {
              obj['mobile'] = ''
            }
            if (data.results[i]['收货人/提货人'] != undefined) {
              let x = data.results[i]['收货人/提货人'].toString()
              obj['name'] = x.trim()
            } else {
              obj['name'] = ''
            }
            if (data.results[i]['详细收货地址/提货地址'] != undefined) {
              let x = data.results[i]['详细收货地址/提货地址'].toString()
              obj['address'] = x.trim()
            } else {
              obj['address'] = ''
            }
            list.push(obj)
          }
          this.daorulog = true
          this.daoruing = true
          axios.post("/api/plan-market/order/info/import", {
              data: list
            })
            .then(function(response) {
              if (response.data.msg.code == 0) {
                if (response.data.data.errors == "") {
                  that.$message.success('导入成功');
                } else {
                  that.success = response.data.data.success
                  that.errors = response.data.data.errors
                  that.centerDialogVisible = true;
                }
              } else {
                that.$message.error(response.data.msg.info);
              }
            })
        }
        if (data.header[0] == "订单编号") {
          for (var i = 0; i < data.results.length; i++) {
            var obj = {}
            if (data.results[i]['订单编号'] != undefined) {
              let x = data.results[i]['订单编号'].toString()
              obj['source_id'] = x.trim()
            } else {
              obj['source_id'] = ''
            }
            if (data.results[i]['联系手机'] != undefined) {
              let x = data.results[i]['联系手机'].toString()
              obj['mobile'] = x.trim()
            } else {
              obj['mobile'] = ''
            }
            if (data.results[i]['收货人姓名'] != undefined) {
              let x = data.results[i]['收货人姓名'].toString()
              obj['name'] = x.trim()
            } else {
              obj['name'] = ''
            }
            console.log(data.results[i]['收货地址'])
            if (data.results[i]['收货地址 '] != undefined) {
              let x = data.results[i]['收货地址 '].toString()
              obj['address'] = x.trim()
            } else {
              obj['address'] = ''
            }
            if (data.results[i]['收货地址'] != undefined) {
              let x = data.results[i]['收货地址'].toString()
              obj['address'] = x.trim()
            }
            list.push(obj)
          }
          this.daorulog = true
          this.daoruing = true
          axios.post("/api/plan-market/order/info/import", {
              data: list
            })
            .then(function(response) {
              if (response.data.msg.code == 0) {
                if (response.data.data.errors == "") {
                  that.$message.success('导入成功');
                } else {
                  that.success = response.data.data.success
                  that.errors = response.data.data.errors
                  that.centerDialogVisible = true;
                }
              } else {
                that.$message.error(response.data.msg.info);
              }
            })
        }
        //导入的导入修改数据
        if (data.header[0] == "*外部订单号") {
          for (var i = 0; i < data.results.length; i++) {
            var obj = {}
            if (data.results[i]['*外部订单号'] != undefined) {
              let x = data.results[i]['*外部订单号'].toString()
              obj['source_id'] = x.trim()
            } else {
              obj['source_id'] = ''
            }
            if (data.results[i]['*身份证号'] != undefined) {
              let x = data.results[i]['*身份证号'].toString()
              obj['id_card_num'] = x.trim()
            } else {
              obj['id_card_num'] = ''
            }
            if (data.results[i]['*身份证姓名'] != undefined) {
              let x = data.results[i]['*身份证姓名'].toString()
              obj['id_card_name'] = x.trim()
            } else {
              obj['id_card_name'] = ''
            }
            list.push(obj)
          }
          this.daorulog = true
          this.daoruing = true
          axios.post("/api/plan-market/outer/order/info", {
              data: list
            })
            .then(function(response) {
              if (response.data.msg.code == 0) {
                if (response.data.data.errors == "") {
                  that.$message.success('导入成功');
                } else {
                  that.success = response.data.data.success
                  that.errors = response.data.data.errors
                  that.centerDialogVisible = true;
                }
              } else {
                that.$message.error(response.data.msg.info);
              }
            })

        }

        //身份证信息导入
        if (data.header[0] == "*外部订单号*") {
          for (var i = 0; i < data.results.length; i++) {
            var obj = {}
            if (data.results[i]['*外部订单号*'] != undefined) {
              let x = data.results[i]['*外部订单号*'].toString()
              obj['source_id'] = x.trim()
            } else {
              obj['source_id'] = ''
            }
            if (data.results[i]['*身份证号*'] != undefined) {
              let x = data.results[i]['*身份证号*'].toString()
              obj['id_card_num'] = x.trim()
            } else {
              obj['id_card_num'] = ''
            }
            if (data.results[i]['*身份证姓名*'] != undefined) {
              let x = data.results[i]['*身份证姓名*'].toString()
              obj['id_card_name'] = x.trim()
            } else {
              obj['id_card_name'] = ''
            }
            list.push(obj)
          }
          this.daorulog = true
          this.daoruing = true

          axios.post("/api/plan-market/order/idcard/import", {
              data: list
            })
            .then(function(response) {
              if (response.data.msg.code == 0) {
                if (response.data.data.errors == "") {
                  that.$message.success('导入成功');
                } else {
                  that.success = response.data.data.success
                  that.errors = response.data.data.errors
                  that.centerDialogVisible = true;
                }
              } else {
                that.$message.error(response.data.msg.info);
              }
            })

        }
      },
      search: function() {
        console.log(this.disid)
      },
      loggo: function() {
        console.log("b")
      },
      //获取页面那数据
      getlist: function() {
        var that = this
        axios.get('/api/plan-market/import/details')
          .then(response => {
            that.list = response.data.data
            that.loading = false
          })
      },
      //下载模板
      uploadTemplete() {
        window.location.href = 'https://file.storep.91haoka.cn/gantanhao/orderuploadxin.xlsx?v='+Math.random()+'&attname='
      },
      photoTemplete2(){
        const a = document.createElement('a');
        a.setAttribute('download', '通过导入复制订单.xlsx');
        a.setAttribute('href', this.url + 'static/feidan.xlsx');
        a.click();
      },
      photoTemplete3:function(){
        const a = document.createElement('a');
        a.setAttribute('href', this.url + 'static/shenfen.xlsx');
        a.click();
        console.log(a)
      },
      pinduoduodown: function() {
        const a = document.createElement('a');
        a.setAttribute('download', '拼多多.xlsx');
        a.setAttribute('href', this.url + 'static/pinduoduo.xlsx');
        a.click();
        console.log(a)
      },
      photoTemplete: function() {
        const a = document.createElement('a');
        a.setAttribute('download', '照片压缩包模板.zip');
        a.setAttribute('href', this.url + 'static/pic.zip');
        a.click();
        console.log(a)
      },
      //查看传入的压缩包详情
      gozip: function(time) {
        this.$router.push({
          path: '/orderDo',
          query: {
            time: time
          }
        });
      },
      //查看失败订单
      gofailorder: function(time, status) {
        this.$router.push({
          path: '/failOrder',
          query: {
            time: time,
            status: status
          }
        });
      },
      //订单上传
      ordergo: function(file) {
        this.loading = true
        this.orderexcle = file.file
        var that = this
        var params = new FormData();
        params.append('file', this.orderexcle);
        axios.post('/api/plan-market/order/import', params)
          .then(data => {
            that.getlist()
            if (data.data.msg.code != 0) {
              this.$message.error(data.data.msg.info);
            } else {
              // var suerror = JSON.stringify(data.data.data.error);
              // var suerrors = {};
              // for (var item in data.data.data.error) {
              //   suerrors[item] = data.data.data.error[item]
              // }

              // this.success = data.data.data.success;
              // this.errors = suerrors
              this.success = data.data.data.success;
              // this.errorsorder = data.data.data.error
              this.centerDialogVisibleorder = true;

              if(data.data.data.error){
                this.errorsorder = []
                for(let index in data.data.data.error){
                    this.errorsorder.push(data.data.data.error[index])
                }
                this.downerrorlist = []
                for(let index in data.data.data.error_data){
                    this.downerrorlist.push(data.data.data.error_data[index])
                }
                console.log(this.downerrorlist)

              }
            }
          })
      },
      //照片上传
      picgo: function(file) {
        this.loading = true
        var that = this
        this.piczip = file.file
        var params = new FormData();
        params.append('file', this.piczip);
        params.append('type', 1);
        axios.post('/api/system/upload_pic_zip', params)
          .then(response => {

            // axios.get('/api/system/qiniu/upload_token?bucket=liubang')
            // 	.then(response => {
            // 	var qiniudata = new FormData();
            // 		qiniudata.append('file', that.piczip);
            // 		qiniudata.append('token',response.data);
            // 		axios.post('https://upload.qiniup.com',qiniudata)
            // 			.then(response => {
            // 				that.getlist()
            // 				that.opensucess()
            // 			})
            // })
            that.getlist()
            that.opensucess()
          })
      },
      openbig: function() {
        this.$message.error("文件太大了，要么压缩一下，要么分几次上传吧！");
      },
      opensucess: function() {
        this.$message({
          message: "恭喜你，上传成功了！",
          type: "success"
        });
      },
      opengeshi: function() {
        this.$message.error("格式不对，请调整一下格式");
      },
      shangchuanexcel: function(file) {
        var na = file.name;
        na = na.split(".");
        console.log(na[na.length - 1]);
        if (na[na.length - 1] == "xlsx") {
          if (file.size > 1024 * 1024 * 50) {
            this.openbig();
            return false;
          } else {}
        } else {
          this.opengeshi();
          return false;
        }
      },
      shangchuanzip: function(file) {
        console.log(file.type);
        if (file.type.indexOf("zip") != -1) {
          if (file.size > 1024 * 1024 * 50) {
            this.openbig();
            return false;
          } else {}
        } else {
          this.opengeshi();
          return false;
        }
      },
      tonocopy(){
        this.$router.push('/orderImport/nocopy')
      }
    }
  }
</script>

<style lang="scss" scoped>
  .index {

      /deep/ .el-select{width: 250px; margin-right: 20px;}
      /deep/ .el-input{width: 250px; margin-right: 20px;}
      /deep/ .el-input__inner{ line-height: 36px; height: 36px; font-size: 12px; background: rgb(247, 248, 250); border: 0;}
      /deep/ .el-range-editor .el-range-input { background: #f7f8fa;}
      /deep/ .el-button{ padding: 11px 23px !important;}

      /deep/ .el-input__inner:-moz-placeholder {
          color: rgb(140, 140, 140);
      }

      /deep/ .el-input__inner:-ms-input-placeholder {
          color: rgb(140, 140, 140);
      }

      /deep/ .el-input__inner::-webkit-input-placeholder {
          color: rgb(140, 140, 140);
      }
      /deep/ .el-button--danger {
          color: #FFF;
          background-color: #2974FF;
          border-color: #2974FF;
      }
      /deep/ .el-button--danger.is-plain {
          color: #EEF7FF !important;
          background: #EEF7FF !important;
          border-color: #18A0FB !important;
      }

      /deep/ .btnp .el-button--primary {
          color: #FFF;
          border-radius: 20px;
          background-color: #2974FF;
          border-color: #2974FF;
      }

        .title {
          border-bottom: 1px #EAEAEA solid;
          line-height: 45px;
          padding-left: 30px;
          font-size: 16px;
          background: #F9F9F9;
          color: #727272
        }

        .add {
          padding: 20px 30px
        }

        .list {
          display: flex;
          text-align: center;
          color: #727272;
          line-height: 45px;
          margin: 0 20px;
          border: 1px #EAEAEA solid;
          margin-top: -1px;

          .th {
            flex: 1;
            background: #F9F9F9;
            font-size: 14px;
            border-right: 1px #EAEAEA solid;
          }

          .td {
            flex: 1;
            font-size: 14px;
            border-right: 1px #EAEAEA solid;
          }
        }

        .el-pagination {
          text-align: right;
          margin: 20px 0
        }



    .el-upload-list {
      width: 138px;
      position: relative
    }

    .result {
      margin-top: 20px;
    }



    .line {
      text-align: center;
    }



    .upload-demo {
      margin-right: 20px;
    }

    /deep/ .el-upload-list {
      position: relative !important;
      height: auto !important;
    }



    /deep/ .el-form-item {
      margin-bottom: 10px !important;
    }

    /deep/ .el-tooltip__popper {
      width: 540px;
      line-height: 30px !important;
    }

    .uploadhou {}

    .uploadhou p {
      margin-bottom: 20px;
    }

    .chenggong {
      color: #42b983;
    }

    .shibai {
      color: #ff0000;
    }

    .ypp {
      line-height: 20px;
      font-size: 14px;
    }

    .dpp {
      margin-left: 20px;
    }

    .content-right {
      flex-basis: 1160px;
    }

    .cur {
      cursor: pointer;
      color: #077ECE;
    }

    .cur2 {
      cursor: pointer;
      color: #f00;
      font-size: 18px;
      text-decoration: underline;
    }

    .develop {}

    .orders {
      background: #FFFFFF;
      padding: 0 20px;
      border-radius: 5px;

    }

    .orderslist {
      display: flex;
      text-align: center;
      line-height: 50px;
      font-size: 14px;
      color: #666666;

    }

    .ordersli {
      flex: 1;
      border: 1px #EAEAEA solid;
      margin-left: -1px;
      margin-top: -1px;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 50px;
      line-height: 20px;
      flex-wrap: wrap;
    }

    .zipnum {
      width: 100%;
    }

    .ordersl {
width: 100%;
      display: flex;
      font-size: 14px;
    }

    .ordersr {

      display: flex;
      font-size: 14px;
    }

    .orderslth {
      background: #EAEAEA;
      color: #333;
      font-size: 14px !important;
    }

    .ordersltr {}

    .develop-top {}

    .orders-alt {
      color: #BFBFBF;
      font-size: 14px;
      line-height: 34px;
    }

    .develop-toptitle {
      color: #707070;
      font-size: 16px;
      line-height: 40px;
      padding-left: 28px;
      background: #e6e6e6;
      color: 1px #d4d4d4 solid;
    }

    .develop-topcon {
      background: #ffffff;
      padding: 15px 0px 0px 26px;
      display: flex;
    }

    .develop-topconleft {}

    .develop-tcltow {
      display: flex;
      padding: 7px 0;
    }

    .develop-tcltow span {}

    .develop-tcldown {

      background: #ffffff !important;
      font-size: 14px;
      color: #29abe2 !important;
      margin-top: 0px;
      line-height: 34px;
      margin-right: 10px;
    }

    .develop-tcldown a {
      color: #29abe2 !important;
      cursor: pointer;
    }

    .develop-tcltowalt {
      color: #707070;
      font-size: 16px;
      margin-top: 22px;
    }

    .develop-topconright {
      display: flex;
      color: #ef2e4f;
      font-size: 16px;
      margin-left: 300px;
      line-height: 34px;
    }

    .develop-chuli {
      color: #29abe2;
      font-size: 16px;
      margin-left: 20px;
    }

    .develop-chuli a {
      color: #29abe2;
    }

    .wenhao {
      margin-top: 5px;
      margin-left: 15px;
      cursor: pointer;
    }

    .develop-center {
      margin-top: 20px;
    }

    .develop-listt {
      background: #ebecef;
      color: #4d4d4d;
      text-align: center;
    }

    .developtr {
      font-size: 16px;
      display: flex;
      justify-content: space-around;
      background: #e5e5e5;
      text-align: center;
      padding: 16px 0;
      color: #4d4d4d;
    }

    .developtr div {
      flex: 1;
    }

    .developtd {
      display: flex;
      justify-content: space-around;
      background: #ffffff;
      text-align: center;
      padding: 15px 0;
      font-size: 14px;
      color: #666666;
      border-bottom: 1px #eaeaea solid;
    }

    .developtd div {
      flex: 1;
    }

    .index-tgfanye {
      display: flex;
      padding: 40px 0;
      justify-content: flex-end;
      line-height: 40px;
      color: #666666;
      font-size: 16px;
      background: #ffffff;
    }

    .index-tginput {
      width: 65px;
      margin-right: 15px;
    }

    .index-tgfanyeg {
      margin-right: 15px;
    }

    .index-tgfanyet {
      margin-right: 15px;
    }

    .index-tgfanyepre {
      margin-right: 15px;
      cursor: pointer;
    }

    .index-tgfanyenext {
      margin-right: 15px;
      cursor: pointer;
    }
    .o_ul {
					background: #f9f9f9;
					display: flex;

					.o_li {
						flex-basis: 160px;
						line-height: 45px;
						display: block;
						color: #727272;
						border-top: 3px #f9f9f9 solid;
						font-size: 14px;
						text-align: center;
					}

					.act {
						border-top: 3px #dd2727 solid;
						background: #ffffff;
					}
				}
  }
</style>
